package spe.ui.properties;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.HashMap;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;

import spe.model.spem.MethodLibrary;
import spe.model.spem.MethodPlugin;
import spe.model.spem.SPEMElement;
import spe.model.spem.SPERootElement;

public class MethodPluginPropertySection extends PropertySection
{	
	//ui components
	private Label lbId;
	private Label lbName;
	private Label lbParentElement;
	private Text txtId;
	private Text txtName;
	private CCombo cboParentElement;
	private TabbedPropertySheetPage tabbedPropertySheetPage;
	
	private SPEMElement[] libraries = null;
	
	private void initComponents(Composite comp)
	{
		lbId = getWidgetFactory().createLabel(comp, "ID");
		txtId = getWidgetFactory().createText(comp, "");
		lbName = getWidgetFactory().createLabel(comp, "Name");
		txtName = getWidgetFactory().createText(comp, "");
		lbParentElement = getWidgetFactory().createLabel(comp, "Parent");
		cboParentElement = getWidgetFactory().createCCombo(comp);
		cboParentElement.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent se)
			{
				//System.out.println(String.format("[MethodPluginPropertySection] widgetSelected"));
				int idx = cboParentElement.getSelectionIndex();
				MethodLibrary ml = (MethodLibrary)(libraries[idx]);
				ml.addMethodPlugin((MethodPlugin) element);
				((MethodPlugin) element).setParent(ml);
			}
		});
		
		FormData fd = new FormData();
		fd.top = new FormAttachment(0, 5);
		fd.left = new FormAttachment(0, 5);
		lbId.setLayoutData(fd);
		
		fd = new FormData();
		fd.top = new FormAttachment(0, 5);
		fd.left = new FormAttachment(lbId, 25);
		fd.right = new FormAttachment(100, -50);
		txtId.setLayoutData(fd);
		
		fd = new FormData();
		fd.top = new FormAttachment(lbId, 5);
		fd.left = new FormAttachment(lbId, 0, SWT.LEFT);
		lbName.setLayoutData(fd);
		
		fd = new FormData();
		fd.top = new FormAttachment(lbName, 0, SWT.TOP);
		fd.left = new FormAttachment(txtId, 0, SWT.LEFT);
		fd.right = new FormAttachment(txtId, 0, SWT.RIGHT);
		txtName.setLayoutData(fd);
		
		fd = new FormData();
		fd.top = new FormAttachment(lbName, 5);
		fd.left = new FormAttachment(lbId, 0, SWT.LEFT);
		lbParentElement.setLayoutData(fd);
		
		fd = new FormData();
		fd.top = new FormAttachment(lbParentElement, 0, SWT.TOP);
		fd.left = new FormAttachment(txtId, 0, SWT.LEFT);
		fd.right = new FormAttachment(txtId, 0, SWT.RIGHT);
		cboParentElement.setLayoutData(fd);
	}
	
	protected void initControlsValues()
	{
		MethodLibrary ml;
		int i=0;
		if(editor != null && (cboParentElement.getItemCount()<1))
		{
			libraries = ((SPERootElement)editor.getRootSPEMElement()).getElement(MethodLibrary.class);
			
			for(i=0; i<libraries.length; i++)
			{
				ml = (MethodLibrary) libraries[i];
				cboParentElement.add(ml.getName());
			}
		}
		//System.out.println(String.format("[MethodPluginPropertySection] Parent %s ", mp.getParent()));
		refresh();
	}
	
	public void createControls(Composite parent, TabbedPropertySheetPage aTabbedPropertySheetPage)
	{
		super.createControls(parent, aTabbedPropertySheetPage);
		tabbedPropertySheetPage = aTabbedPropertySheetPage;
		final Composite composite = getWidgetFactory().createFlatFormComposite(parent);
		
		initComponents(composite);
	}
	
	@Override
	public void refresh()
	{
		int i;
		if(element != null)
		{
			txtId.setText(element.getId());
			txtName.setText(element.getName());
			String[] strItems = cboParentElement.getItems();
			SPEMElement ml = element.getParent();
			
			if(ml != null)
			{
				//System.out.println(String.format("Vao if"));
				for(i=0; i<strItems.length; i++)
				{
					String s = strItems[i];
					if(s.equals(ml.getName()))
					{
						cboParentElement.select(i);
						return;
					}
				}
			}
			cboParentElement.select(-1);
		}
	}

	@Override
	public void propertyChange(PropertyChangeEvent ev)
	{
		refresh();
	}
}
